﻿export default {
   get_user_id_url:function () {
    return location.search.split("user_id=")[1];
   },
    get_user_id: function () {
       let user_id = location.search.split("user_id=")[1];

        if (!!user_id) {
            return user_id
        } else {
            //app
            if (!sessionStorage.getItem("user_id")) {
                return localStorage.getItem("user_id");
            } else {
                return sessionStorage.getItem("user_id");
            }
        }
    },
    get_user_or_ad_id: function () {
        let user_id = location.search.split("user_id=")[1];
        console.log("user_id++++++++++++++++++++")
        console.log(user_id);
        if (!user_id) {
            return sessionStorage.getItem("ad_id")
        } else {
            return user_id
        }
    },
    get_base_url: function () {
        return "https://wx.yizhiweimeng.com";
    },
    jump: function (obj={url:"",id:"",show:{aniShow:"none"}}) {
        if(!!obj.show){
          obj.show = {
            autoShow: true,//页面loaded事件发生后自动显示，默认为true
            aniShow: 'none',//页面显示动画，默认为”slide-in-right“；
          }
        }
        mui.plusReady(function () {
            console.log("当前页面URL：" + plus.webview.currentWebview().getURL());
            mui.openWindow({
                url: obj.url,
                id: obj.id,
                show:obj.show
            });
        })
    },
    audio: function () {

    },
    scroll_c_opacity: function (el, opt_el, step) {


    },
   //收藏
    collect_c:(obj, Indicator,qs,_this)=>{
        Indicator.open();
        _this.$http.post('/yizhiweimen/api/user/collect/insertCollect',
            qs.stringify(
                {
                    'sortId': obj.sort,
                    "userId": obj.user_id,
                    "collectId": obj.collect_id
                })
            )
            .catch((thrown) => {
                Indicator.close();
            }).then((e) => {
                Indicator.close();
                //this.list_data = e.data;
            })
    },
    //wx_分享
    wx_share: function () {


    },
  go_back_none_btn:function(){
    window.history.back(-1);
  },
  go_back:function (_this) {
      _this.$refs.back_btn.addEventListener("click",function () {
        window.history.back(-1);
      })
  },
  go_b:function(){
    window.history.back(-1);
  },
  //获取图片
  get_img:function (_this,cb = ()=>{}) {
    _this.$refs.file.addEventListener("change", (e) => {
      //promise s
      let count = 0;
      let result = e.srcElement.files;

      //限制张数
      if (e.srcElement.files.length > 1) {
        Toast({
          message: '图像张数不能多于1张',
          position: 'bottom',
          duration: 2000
        });
        return;
      }

      let reader = new FileReader();//读取图片
      reader.readAsDataURL(result[0]);//get img url
      let _e = e;

      //可以获取啦呵呵
      reader.onload = (e) => {
        _this.show_b_img_data = e.target.result;
        cb()
      }
    });
  },
  //录音
  get_voice: function (wx,_this,qs) {
    _this.add_s = false;

    wx.startRecord();

    wx.onVoiceRecordEnd({
      complete: (res) => {
        //alert("录音完成");
        //语音
        _this.record_voice = res.localId;
        this.play_music(wx,_this);
        //出现删除语音标志 和 动画状态

        this.wx_upload_voice(_this.qs)
      }
    });
  },
  //播放音乐
  //播放音乐
  play_music: function (wx,_this) {
    wx.playVoice({
      localId: _this.record_voice // 需要播放的音频的本地ID，由stopRecord接口获得
    });
  },
  //wx上传语言
  wx_upload_voice:function(_this,qs){
    _this.$http.post('/yizhiweimen/api/wxUtil/downloadMedia',
      qs.stringify(
        {
          "mediaId": _this.record_voice,
        })
    )
      .catch((thrown) => {
        console.log(thrown);
      }).then((e) => {
        _this.voice_data = e.data;

    })
  },
  //wx config voice
  wx_config_voice:function (_this,qs,Indicator,wx) {
    Indicator.open();
    let url = location.href;
    url = url.split("#")[0]
    _this.$http.post('/yizhiweimen/api/wxUtil/getSign',
      qs.stringify(
        {
          "url": url
        })
    )
      .then((e) => {
        // 上面两个请求都完成后，才执行这个回调方法
        Indicator.close();
        let data = e.data;
        wx.config({
          debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
          appId: data.appId, // 必填，企业号的唯一标识，此处填写企业号corpid
          timestamp: data.timestamp, // 必填，生成签名的时间戳
          nonceStr: data.nonceStr, // 必填，生成签名的随机串
          signature: data.signature,// 必填，签名，见附录1
          jsApiList: ["startRecord", "stopRecord", "onVoiceRecordEnd", "playVoice", "pauseVoice", "stopVoice", "onVoicePlayEnd", "uploadVoice"] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
        });
      })
      .catch((e) => {
        Indicator.close();
        console.log(e)

      })
  },
  //is_app
  is_app:function (_this) {
    try{
      if(localStorage.getItem("is_app") == true ||localStorage.getItem("is_app") == "true"){
         _this.base_url =this.get_base_url();
       // _this.$set(_this.data,"base_url",this.get_base_url());
        //this.base_url = this.
        return true
      }else{
        _this.base_url="";
       return false
      }
    }catch (e){
      console.log(e)
      _this.base_url="";
     return false
   }
  },
  change_header:function(){
    if(localStorage.getItem("is_app") == true ||localStorage.getItem("is_app") == "true"){
      var header = document.createElement("div");
      var app = document.getElementById("app");
      header.style.height = "5.3vw";
      document.getElementsByTagName("body")[0].insertBefore(header,app);
    }
  },
  app_add_header_h:function(){
    if(localStorage.getItem("is_app") == true ||localStorage.getItem("is_app") == "true"){
      var header = document.createElement("div");
      var app = document.getElementById("app");
      header.style.height = "5.3vw";
      document.getElementsByTagName("body")[0].insertBefore(header,app);
    }
  },
  //app 录音
  app_recoed: function (_this) {

    let r = plus.audio.getRecorder();
    r.record({ filename: "_doc/audio/" }, function (e) {
        console.log("recoed_seccess");
       //up_load(e);
        console.log(JSON.stringify(e));
        _this.app_record_voice = e;//记录
    }, function (e) {
        alert("Audio record failed: " + e.message);
    });
  },
  app_play_voice:function(url){
    mui.plusReady(function () {
        let p = plus.audio.createPlayer(url);
        p.play(function () {
            console.log("play_voice_sus");
        }, function (e) {
            console.log("voice fail");
        });
    })
  },
  //提交voice
  up_load: function(e,fail){
    var task = plus.uploader.createUpload("https:wx.yizhiweimeng.com/yizhiweimeng/api/user/file/upload",
        { method: "POST" }, function (t, status) {
            //上传完成
            if (status == 200) {
                alert("Upload success: " + t.url);
                return {
                    t: t,
                    s: status
                }
            } else {
                fail()
            }
        });
    task.addFile(e, { key: "file" });
    task.start();
  },
  //错误获取
  watch_err:function () {
    console.log("error")
    window.onerror = function (msg, url, lineNo, columnNo, error) {
      console.log("msg",msg);
      console.log("url",url);
      let string = msg.toLowerCase();
      let substring = "script error";
      if (string.indexOf(substring) > -1){
        console.log('Script Error: See Browser Console for Detail');
      } else {
        let message = [
          'Message: ' + msg,
          'URL: ' + url,
          'Line: ' + lineNo,
          'Column: ' + columnNo,
          'Error object: ' + JSON.stringify(error)
        ].join(' - ');

        console.log(message,"color:red");
      }

      // return false;
    };
  },
  //上传文件
  up_load_file:function (_this,formdata,cb = ()=>{}) {
    return  new Promise((resolve, reject) => {
      _this.$http.post('/yizhiweimen/api/user/activity/upload',
        formdata
      )
        .catch((thrown) => {
          console.log(thrown)
          reject();
        }).then((e) => {
        _this.$t_Indicator.close();
        cb();
        resolve(e)
      })
    })
  },
  //$t_Indicator
  this_fix:function (_this,list) {
    _this.$t_Indicator = list[0];
  }
}
